<template>
	<view class="tui-safe-area">

		<view class="tui-address">
			<block v-for="(item,index) in myAddressList" :key="index">
				<tui-list-cell padding="0">
					<view class="tui-address-flex">
						<view class="tui-address-left" @click="doPickAddress(item.id)">
							<view class="tui-address-main">
								<view class="tui-address-name tui-ellipsis">{{item.name}}</view>
								<view class="tui-address-tel">{{item.phone}}</view>
							</view>
							<view class="tui-address-detail">
								<view class="tui-address-label" v-if="item.is_home == 1">默认</view>
								<text>{{item.address_all}}</text>
							</view>
						</view>
						<view class="tui-address-imgbox" @click="goMyAddressEdit(item.id)">
							<image class="tui-address-img" src="/static/images/mall/my/icon_addr_edit.png" />
						</view>
					</view>
				</tui-list-cell>
			</block>
		</view>
		<!-- 新增地址 -->
		<view class="tui-address-new">
			<tui-button type="green" height="88rpx" @click="goMyAddressAdd()">+ 新增收货地址</tui-button>
		</view>
		
		<footer-menu :tabCur="2"></footer-menu>
	</view>
</template>

<script>
	import footerMenu from "@/components/footer-menu.vue"
	import tuiIcon from "@/components/thorui/tui-icon/tui-icon.vue"
	import tuiButton from "@/components/thorui/tui-button/tui-button"
	import tuiListCell from "@/components/thorui/tui-list-cell/tui-list-cell"
	export default {
		components: {
			tuiButton,
			tuiListCell,
			tuiIcon,
			footerMenu
		},
		data() {
			return {
				myAddressList: [],
				prepayId: 0,
				goodsId: 0,
				jumpUrl: '',
			}
		},
		
		methods: {
			
			goMyAddressAdd: function(){
				uni.navigateTo({
					url: '/pages/my-address/add?goods_id='+ this.goodsId
				});
			},
			
			goMyAddressEdit: function(e){
				uni.navigateTo({
					url: '/pages/my-address/edit?id='+ e + '&goods_id=' + this.goodsId
				});
			},
			
			getMyAddressList: function() {
				uni.request({
				    url: this.$apiUrl + '/api/user/addressList',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
						'shopId': uni.getStorageSync('shopId'),
					},
					success: (res) => {
						this.myAddressList = res.data['data'];
				    }
				});
			},
			
			//预支付订单的时候选择地址
			doPickAddress: function(e) {
				if(this.jumpUrl) {
					uni.setStorageSync('userAddressId', e);
					uni.navigateTo({
						url: this.jumpUrl
					});
					return ;
				}else{
					uni.navigateBack();
					return;
				}
			},
		},
		
		onLoad: function(options) {
			this.prepayId = options.prepay_id || 0;
			this.goodsId = options.goods_id || 0;
			this.jumpUrl = options.jump_url || '';
			this.getMyAddressList();
		},
	}
</script>

<style>
	
	.tui-address {
		width: 100%;
		padding-top: 20rpx;
		padding-bottom: 160rpx;
	}
	.tui-address-flex {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.tui-address-main {
		width: 600rpx;
		height: 88rpx;
		display: flex;
		font-size: 30rpx;
		line-height: 88rpx;
		padding-left: 30rpx;
	}

	.tui-address-name {
		width: 300rpx;
		height: 70rpx;
	}

	.tui-address-tel {
		margin-left: 10rpx;
	}

	.tui-address-detail {
		font-size: 24rpx;
		word-break: break-all;
		padding-bottom: 25rpx;
		padding-left: 25rpx;
		padding-right: 120rpx;
	}

	.tui-address-label {
		padding: 5rpx 8rpx;
		flex-shrink: 0;
		background: #e41f19;
		border-radius: 6rpx;
		color: #fff;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		font-size: 25rpx;
		line-height: 25rpx;
		transform: scale(0.8);
		transform-origin: center center;
		margin-right: 6rpx;
	}

	.tui-address-imgbox {
		width: 80rpx;
		height: 100rpx;
		position: absolute;
		display: flex;
		justify-content: center;
		align-items: center;
		right: 10rpx;
	}

	.tui-address-img {
		width: 36rpx;
		height: 36rpx;
	}

	.tui-address-new {
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 100rpx;
		z-index: 999;
		padding: 20rpx 25rpx 30rpx;
		box-sizing: border-box;
		background: #fafafa;
	}

	.tui-safe-area {
		padding-bottom: env(safe-area-inset-bottom);
	}
</style>
